#include "../skyAdapter.h"

#ifdef _SYMBIAN
#include "../skyTimer.h"
namespace Sky
{
//typedef TInt (*aFunction)(TAny* aPtr);
TBool Timer::CreateTimer(TInt MicroSeconds,TimerCallback cb,TBool bRepeat )
{
	if(!m_pTimer)
	{
		m_pTimer = CPeriodic::NewL(CActive::EPriorityHigh);

		m_pTimer->Start(MicroSeconds, MicroSeconds, TCallBack(Timer::Run,this));

	}
	CallbackNode node;
	node._bRepeat = bRepeat;
	node._cb = cb;
	node._interal = MicroSeconds;
	node._left = MicroSeconds;
	_list.append(node);
	return vTrue;
}

TVoid Timer::onTimer()
{
	CBList::iterator itr;
	const CallbackNode* node;
	CallbackNode* pNode;
	for(itr = _list.getHead(); itr != vNull; ++itr)
	{
		node = itr;
		pNode = const_cast<CallbackNode*>(node);
		pNode->_left = 0;
		if(pNode->_left == 0)
		{
			(pNode->_cb)();
			if(pNode->_bRepeat)
				pNode->_left = pNode->_interal;
			else
				_list.remove(itr);
		}
	}
}
TInt Timer::Run( void * aPtr )
{
	((Timer*)aPtr )->onTimer();
}

TVoid Timer::DeleteTimer(TimerCallback callback)
{
	CBList::iterator itr;
	const CallbackNode* node;
	CallbackNode* pNode;
	for(itr = _list.getHead(); itr != vNull;)
	{
		node = itr;
		pNode = const_cast<CallbackNode*>(node);
		if(pNode->_cb == callback)
		{
			_list.remove(itr);
		}
	}
	return;
}

OSTimer::~OSTimer()
{
	
}
Timer::~Timer()
{
	if(m_pTimer)
	{
		m_pTimer->Cancel();
		delete m_pTimer;
		m_pTimer = vNull;
	}
}
}
#endif
